package csm.web.core.search.mashup.naver;

import java.io.IOException;
import java.util.List;

import org.jdom.Element;
import org.jdom.JDOMException;

import csm.common.StringUtil;
import csm.common.xml.XmlDocument;
import csm.web.core.search.BriefItem;
import csm.web.core.search.SearchResultInfo;

public class NaverSearchResult  extends SearchResultInfo {

	@Override
	public void parseResult(String responseString)
	{
		int hitcount = 0;
		int pageSize = 10;
		int page = 1;
		int startRow=0;
		BriefItem item;

		if(StringUtil.isNullOrEmpty(responseString)) return;
		setResultString(responseString);

		XmlDocument xmlDoc = null;
		try
		{
			responseString.replaceAll("&\\#x[0-9][A-F]?;", " ");
			xmlDoc = new XmlDocument(responseString);
			if(xmlDoc.selectSingleNodeText("//error") != null){
				String errMsg = StringUtil.format("Error raise on Naver Search : {1}, {2}, {3}"
						, xmlDoc.selectSingleNodeText("//error")
						, xmlDoc.selectSingleNodeText("//error/error_code")
						, xmlDoc.selectSingleNodeText("//error/message"));
				this.setRaisedError(true);
				this.setErrorMessage(errMsg);
				return;
			}

			hitcount = Integer.parseInt( xmlDoc.selectSingleNodeText("//rss/channel/total") );
			if( !StringUtil.isNullOrEmpty(xmlDoc.selectSingleNodeText("//rss/channel/start")))
				startRow = Integer.parseInt( xmlDoc.selectSingleNodeText("//rss/channel/start") );

			if( !StringUtil.isNullOrEmpty(xmlDoc.selectSingleNodeText("//rss/channel/display")))
				pageSize = Integer.parseInt( xmlDoc.selectSingleNodeText("//rss/channel/display") );

			if( pageSize <= 0)
				pageSize = 10;
			page = (startRow/pageSize) + 1; // 페이지로 변환계산해야 함.

			this.setHitCount(hitcount);
			this.setPageSize(pageSize);
			this.setPage(page);

			List<?> list = xmlDoc.selectNodes("//rss/channel/item");
			for (int i = 0 ; i < list.size(); i++) {
				Element el = (Element)list.get(i);
				item = new BriefItem();
				item.add("title", el.getChildText("title"));
				item.setTitle(el.getChildText("title"));

				item.add("author", el.getChildText("author"));
				item.setAuthor(el.getChildText("author"));

				item.add("publisher", el.getChildText("publisher"));
				item.setPublisher(el.getChildText("publisher"));

				item.add("pubdate", el.getChildText("pubdate"));
				item.setPubYear(el.getChildText("pubdate"));

				if(!StringUtil.isNullOrEmpty(el.getChildText("thumbnail"))){
					item.add("thumbnail", el.getChildText("thumbnail"));
					item.setThumbNailUrl(el.getChildText("thumbnail"));
				}
				else{
					item.add("image", el.getChildText("image"));
					item.setThumbNailUrl(el.getChildText("image"));
				}

				item.add("isbn", el.getChildText("isbn"));
				item.setIsbn(el.getChildText("isbn"));

				item.add("link", el.getChildText("link"));
				item.setLink(el.getChildText("link"));

				item.add("price", el.getChildText("price"));
				item.setPrice(el.getChildText("price"));

				item.add("description", el.getChildText("description"));
				item.setDescription(el.getChildText("description"));

				this.addBriefItem(item);
			}

		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
